import { debounce } from '@utils/function'
import { useRef } from 'react'

const useUpdateMutation = (mutationApiHook, options = {}) => {
  if (mutationApiHook === undefined) return

  const { onSuccess, debounceDelay = 500 } = options

  const { mutate } = mutationApiHook()

  const onUpdate = (data) => mutate(data, { onSuccess })

  const debounceUpdate = useRef(debounce(onUpdate, debounceDelay)).current

  return {
    mutate,
    onUpdate,
    debounceUpdate,
  }
}

export default useUpdateMutation
